import { fetchList } from '@/api/api'

const order = {
  state: {
    weidaohuolist: [],
    newlist: [],
    yidaohuolist: []
  },

  mutations: {
    SET_WEIDAO_LIST: (state, list) => {
      state.weidaohuolist = list
    },
    SET_YIDAO_LIST: (state, list) => {
      state.yidaohuolist = list
    },
    SET_NEW_LIST: (state, list) => {
      console.log(list)
      state.newlist = list
    }
  },

  actions: {
    // 用户名登录
    refreshWeidaohuo({ commit }, username) {
      const param = { query: 'vue_供货商未到货list', username: username }
      return new Promise((resolve, reject) => {
        fetchList(param).then(({ data }) => {
          console.log(data)
          if (data.flag === '1') {
            commit('SET_WEIDAO_LIST', data.data)
          }
          if (data.message === 'null') {
            commit('SET_WEIDAO_LIST', [])
          }
          resolve()
        })
      })
    },
    refreshYidaohuo({ commit }, username) {
      const param = { query: 'vue_供货商已到货list', username: username }
      return new Promise((resolve, reject) => {
        fetchList(param).then(({ data }) => {
          if (data.flag === '1') {
            commit('SET_YIDAO_LIST', data.data)
          }
          if (data.message === 'null') {
            commit('SET_YIDAO_LIST', [])
          }
          resolve()
        })
      })
    },
    refreshNew({ commit }, username) {
      const param = {
        query: 'vue_供货商订单list',
        username: localStorage.ghusername,
        订单状态: '11,15'
      }
      return new Promise((resolve, reject) => {
        fetchList(param).then(({ data }) => {
          console.info(data)
          if (data.flag === '1') {
            commit('SET_NEW_LIST', data.data)
          }

          if (data.message === 'null') {
            commit('SET_NEW_LIST', [])
          }
          resolve()
        })
      })
    }
  }
}

export default order
